<?php
/**
 * script to read XML string from an entered report
 * Joaquin Olvera 11/11/2011
 */

require_once 'common/connectdb.php';
require 'common/common.php';

$xml = file_get_contents('php://input');
/*
$xml = '<REPORT>
<TITLE>super prueba 2</TITLE>
<DESCRIPTION>Description...		</DESCRIPTION>
<DATE>2011-11-0</DATE>
<USER_ID>1</USER_ID>
<COORDINATES> 21-18-02.54891 N 157-50-45.90280 W</COORDINATES>
<TYPE>1</TYPE>
<ADDRESS>gas</ADDRESS>
<CARS>
	<CAR>
		<PLATE>1234</PLATE>
		<BRAND>Hummer</BRAND>
		<MODEL>H3</MODEL>
		<YEAR>2011</YEAR>
		<COLOR>Negro</COLOR>
		<DRIVER_ID>xyz</DRIVER_ID>
		<DRIVER_NAME>Joa</DRIVER_NAME>
	</CAR>
</CARS>
</REPORT>';
*/

/*
$xml = '<REPORT>
	<TITLE>Accidente Puente Altaria</TITLE>
	<DESCRIPTION>Accidente entre 2 automoviles en el paso a desnivel por averia en la direccion de uno de los involucrados.</DESCRIPTION>
	<DATE>2011-11-09</DATE>
	<USER_ID>1</USER_ID>
	<COORDINATES> 21-18-02.54891 N 157-50-45.90280 W</COORDINATES>
	<TYPE>1</TYPE>
	<ADDRESS>Carretera a Zacatecas S/N A la altura de Altaria</ADDRESS>
	<CARS>
		<CAR>
			<PLATE>ZXC1212</PLATE>
			<BRAND>BMW</BRAND>
			<MODEL>M5</MODEL>
			<YEAR>2010</YEAR>
			<COLOR>Azul</COLOR>
			<DRIVER_ID>A123456</DRIVER_ID>
			<DRIVER_NAME>Brian OConner</DRIVER_NAME>
		</CAR>
		<CAR>
			<PLATE>VBN0987</PLATE>
			<BRAND>Mercedes Benz</BRAND>
			<MODEL>Clase SLK</MODEL>
			<YEAR>2008</YEAR>
			<COLOR>Gris</COLOR>
			<DRIVER_ID>B105591</DRIVER_ID>
			<DRIVER_NAME>Toretto</DRIVER_NAME>
		</CAR>
	</CARS>
</REPORT>';

*/

$arrReport = simpleXMLToArray($xml);
$ConnectDB = new ConnectDB();

$title = $arrReport["TITLE"];
$description = $arrReport["DESCRIPTION"];
$date = $arrReport["DATE"];
$user_id = $arrReport["USER_ID"];
$coordinates = $arrReport["COORDINATES"];
$type = $arrReport["TYPE"];
$address = $arrReport["ADDRESS"];
$image = $arrReport["imageID"];

$qReport = "INSERT INTO tb_reports (report_title, report_description, report_date, user_id, report_coordinates, reportType_id, report_address,image_id)
			VALUES ('$title', '$description', '$date', '$user_id', '$coordinates', '$type', '$address','$image')";
$rReport = $ConnectDB->run_query($qReport, TRUE);
$ReportID = $rReport[1];

$arrCars = $arrReport["CARS"];
$cars = $arrCars["CAR"];

foreach ($arrCars as $car)
{
	$plate = $car["PLATE"];
	$brand = $car["BRAND"];
	$model = $car["MODEL"];
	$year = $car["YEAR"];
	$color = $car["COLOR"];
	$driver_id = $car["DRIVER_ID"];
	$driver_name = $car["DRIVER_NAME"];
	
	$qVerifCar = "SELECT car_id FROM tb_cars WHERE car_plate='$plate'";
	$rVerifCar = $ConnectDB->run_query($qVerifCar);
	
	if($rwVerifCar = mysql_fetch_row($rVerifCar)){
		$CarID = $rwVerifCar[0];		
	} else {
		$qCar = "INSERT INTO tb_cars (car_plate, car_brand, car_model, car_year, car_color, carDriver_id, carDriver_name)
			VALUES ('$plate', '$brand', '$model', '$year', '$color', '$driver_id', '$driver_name')";
		$rCar = $ConnectDB->run_query($qCar, TRUE);
		$CarID = $rCar[1];
	}
	$qInsRepCar = "INSERT INTO tb_report_car (report_id, car_id)
						VALUES ('$ReportID', '$CarID')";
	$rInsRepCar = $ConnectDB->run_query($qInsRepCar);

}

?>
